package com.test.day02;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: 使用lock解决线程安全问题
 * @author: luhd
 * @create: 2021-07-31
 */
public class Lock implements Runnable{
    private int ticket = 100;

    //Lock l = new Lock();
    ReentrantLock l = new ReentrantLock();

    @Override
    public void run() {
        while(true){
            l.lock();
            if(ticket >0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"----->正在卖第"+ticket+"张票");
                ticket --;
            }
            l.unlock();
        }
    }
}
